package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author dongliang
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper,CourseTeacher> implements CourseTeacherService {


    @Autowired
    private CourseBaseService courseBaseService;

    //查询
    public List<CourseTeacherDTO> queryTeacherList(Long companyId, Long courseBaseId) {


        if (ObjectUtils.isEmpty(companyId)||
                ObjectUtils.isEmpty(courseBaseId)
        ) {
            ExceptionCast.cast(ContentErrorCode.E_120018);
        }

        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseBase::getCompanyId,companyId);
        queryWrapper.eq(CourseBase::getId,courseBaseId);

        CourseBase courseBase = courseBaseService.getOne(queryWrapper);
        Long id = courseBase.getId();

        LambdaUpdateWrapper<CourseTeacher> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,id);
        List<CourseTeacher> courseTeacherList = this.list(wrapper);

        List<CourseTeacherDTO> courseTeacherDTOList = CourseBaseConvert.INSTANCE.pos2dtos(courseTeacherList);

        return courseTeacherDTOList;
    }

    /**
     * 创建或修改课程计划信息
     *
     * @param dto 课程计划信息
     * @return TeachplanDTO 课程计划信息
     */
    @Transactional
    public CourseTeacherDTO createOrModifyTeachPlan(CourseTeacherDTO dto, Long companyId) {
    /*
     * 业务分析：
            //1.判断业务操作 判断是创建还是修改：teachplanId 如果有:修改 如果没有:创建*/
        CourseTeacherDTO resutlDTO = null;
        if (ObjectUtils.isEmpty(dto.getCourseTeacherId())) {
            //创建
            resutlDTO = createTeachplan(dto, companyId);
        } else {
            //修改
            resutlDTO = modifyTeachplan(dto, companyId);
        }
        return resutlDTO;
    }

    //修改
    private CourseTeacherDTO modifyTeachplan(CourseTeacherDTO dto, Long companyId) {

        //业务判断
        vervify(dto, companyId);

        CourseTeacher courseTeacher = CourseBaseConvert.INSTANCE.dto2po(dto);

        this.baseMapper.updateById(courseTeacher);

        CourseTeacherDTO courseTeacherDTO = getLastCourseTeacherDTO(courseTeacher);

        return courseTeacherDTO;
    }

    private void vervify(CourseTeacherDTO dto, Long companyId) {
        //关键信息判断
        if (ObjectUtils.isEmpty(dto.getCourseId())||
        ObjectUtils.isEmpty(dto.getTeacherName())||
        ObjectUtils.isEmpty(dto.getPhotograph())||
        ObjectUtils.isEmpty(dto.getPosition())||
        ObjectUtils.isEmpty(companyId)){
          ExceptionCast.cast(ContentErrorCode.E_1200503);
        }

        //业务判断
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getId, dto.getCourseId());
        wrapper.eq(CourseBase::getCompanyId, companyId);
        wrapper.eq(CourseBase::getStatus,CommonEnum.USING_FLAG.getCodeInt());

        CourseBase courseBase = this.courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_120024);
        }

    }


    /**
     * 创建课程计划信息
     *
     * @param dto 课程计划信息
     * @return TeachplanDTO 课程计划信息
     */
    private CourseTeacherDTO createTeachplan(CourseTeacherDTO dto, Long companyId) {

        //业务判断
        vervify(dto, companyId);

        CourseTeacher courseTeacher = CourseBaseConvert.INSTANCE.dto2po(dto);

        courseTeacher.setCourseId(dto.getCourseId());
        courseTeacher.setCreateDate(LocalDateTime.now());

        boolean courseTeaResult = this.save(courseTeacher);
        if (!courseTeaResult){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }

        CourseTeacherDTO courseTeacherDTO = getLastCourseTeacherDTO(courseTeacher);

        return courseTeacherDTO;
    }

    private CourseTeacherDTO getLastCourseTeacherDTO(CourseTeacher courseTeacher) {
        Long id = courseTeacher.getId();
        CourseTeacher teacher = this.getById(id);
        CourseTeacherDTO courseTeacherDTO = CourseBaseConvert.INSTANCE.entity2dto(teacher);
        return courseTeacherDTO;
    }

    //教师删除
    public void removeCourseTeacher(Long companyId, Long courseBaseId, Long courseTeacherId) {
        if (ObjectUtils.isEmpty(companyId)||
                ObjectUtils.isEmpty(courseBaseId)||
                ObjectUtils.isEmpty(courseTeacherId)){
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getCompanyId,companyId);
        wrapper.eq(CourseBase::getId,courseBaseId);
        CourseBase courseBase = this.courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        Long baseId = courseBase.getId();
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,baseId);
        queryWrapper.eq(CourseTeacher::getId,courseTeacherId);

        boolean flag = this.remove(queryWrapper);
        if(!flag){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
    }
}
